<?php

/**
 * VehiculoVenta form base class.
 *
 * @method VehiculoVenta getObject() Returns the current form's model object
 *
 * @package    toyomotor
 * @subpackage form
 * @author     Ing. Roberto Hernández De La Luz at Netosfera
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseVehiculoVentaForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id_vehiculo_venta'   => new sfWidgetFormInputHidden(),
      'modelo'              => new sfWidgetFormInputText(),
      'descripcion'         => new sfWidgetFormTextarea(),
      'descripcion_resumen' => new sfWidgetFormTextarea(),
      'id_ano_modelo'       => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('AnoModelo'), 'add_empty' => false)),
      'status'              => new sfWidgetFormInputCheckbox(),
      'in_menu'             => new sfWidgetFormInputCheckbox(),
      'foto1'               => new sfWidgetFormInputText(),
      'foto2'               => new sfWidgetFormInputText(),
      'foto3'               => new sfWidgetFormInputText(),
      'foto4'               => new sfWidgetFormInputText(),
      'foto5'               => new sfWidgetFormInputText(),
      'foto6'               => new sfWidgetFormInputText(),
      'foto7'               => new sfWidgetFormInputText(),
      'archivo'             => new sfWidgetFormInputText(),
      'created_at'          => new sfWidgetFormDateTime(),
      'colores_list'        => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Color')),
    ));

    $this->setValidators(array(
      'id_vehiculo_venta'   => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id_vehiculo_venta')), 'empty_value' => $this->getObject()->get('id_vehiculo_venta'), 'required' => false)),
      'modelo'              => new sfValidatorString(array('max_length' => 30)),
      'descripcion'         => new sfValidatorString(array('max_length' => 2500)),
      'descripcion_resumen' => new sfValidatorString(array('max_length' => 300)),
      'id_ano_modelo'       => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('AnoModelo'))),
      'status'              => new sfValidatorBoolean(array('required' => false)),
      'in_menu'             => new sfValidatorBoolean(array('required' => false)),
      'foto1'               => new sfValidatorString(array('max_length' => 70)),
      'foto2'               => new sfValidatorString(array('max_length' => 70, 'required' => false)),
      'foto3'               => new sfValidatorString(array('max_length' => 70, 'required' => false)),
      'foto4'               => new sfValidatorString(array('max_length' => 70, 'required' => false)),
      'foto5'               => new sfValidatorString(array('max_length' => 70, 'required' => false)),
      'foto6'               => new sfValidatorString(array('max_length' => 70, 'required' => false)),
      'foto7'               => new sfValidatorString(array('max_length' => 70, 'required' => false)),
      'archivo'             => new sfValidatorString(array('max_length' => 70, 'required' => false)),
      'created_at'          => new sfValidatorDateTime(),
      'colores_list'        => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Color', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('vehiculo_venta[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'VehiculoVenta';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['colores_list']))
    {
      $this->setDefault('colores_list', $this->object->Colores->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveColoresList($con);

    parent::doSave($con);
  }

  public function saveColoresList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['colores_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Colores->getPrimaryKeys();
    $values = $this->getValue('colores_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Colores', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Colores', array_values($link));
    }
  }

}
